package com.hellogood.yaohuo.server.mgr;

import java.util.ArrayList;
import java.util.List;

import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.Org;
import com.hellogood.yaohuo.shared.entity.User;

public class OrgMgr {
	public String getOrgCodeByUserQuyu(String userCode, String quyuCode) {
		List<String> orgCodeList = new ArrayList<String>();
		String org = "";
		if (userCode.equals("xsa")) {
			List<Org> orgList = DAOFactory.getInstance().createOrgDAO().getOrgListNotC();
			for (Org o : orgList) {
				orgCodeList.add(o.getOrgCode());
			}
		} else {
			orgCodeList = DAOFactory.getInstance().createOrgDAO().getOrgByUserQuyu(userCode, quyuCode);
		}
		for (String str : orgCodeList) {
			org = org + "," + str;
		}
		if (org.length() > 0) {
			if (org.substring(0, 1).equals(",")) {
				org = org.substring(1, org.length());
			}
		}
		return org;
	}

	public String getOrgCodeByUser(String userCode) {
		String orgs = "";
		List<Org> orgList = new ArrayList<Org>();
		if (userCode.equals("xsa")) {
			orgList = DAOFactory.getInstance().createOrgDAO().getOrgListNotC();
		} else {
			orgList = DAOFactory.getInstance().createOrgDAO().getOrgListByUserCode(userCode);
		}
		for (Org org : orgList) {
			orgs = orgs + "," + org.getOrgCode();
		}
		if (orgs.length() > 0) {
			if (orgs.substring(0, 1).equals(",")) {
				orgs = orgs.substring(1, orgs.length());
			}
		}
		return orgs;
	}

	public List<Org> getOrgListByUserCode(String userCode) {
		List<Org> orgList = new ArrayList<Org>();
		if (userCode.equals("xsa")) {
			orgList = DAOFactory.getInstance().createOrgDAO().getOrgListWithoutC();
		} else {
			orgList = DAOFactory.getInstance().createOrgDAO().getOrgListByUserWithoutC(userCode);
		}
		return orgList;
	}

	public List<Org> getCOrgList() {
		List<Org> orgList = new ArrayList<Org>();
		orgList = DAOFactory.getInstance().createOrgDAO().getCOrgList();
		return orgList;
	}

	public List<Org> getOrgList() {
		List<Org> orgList = new ArrayList<Org>();
		orgList = DAOFactory.getInstance().createOrgDAO().getOrgListWithoutC();
		return orgList;
	}

	public List<Org> getOrgListByUser(User user) {
		List<Org> orgList = new ArrayList<Org>();
		orgList = DAOFactory.getInstance().createOrgDAO().getOrgListByUserCode(user.getStaffCode());
		return orgList;
	}

	public List<Org> getUnQuyuedOrgList() {
		List<Org> orgList = new ArrayList<Org>();
		orgList = DAOFactory.getInstance().createOrgDAO().getUnQuyuedList();
		return orgList;
	}

	public List<Org> getOrgListByQuyuCode(String code) {
		List<Org> orgList = new ArrayList<Org>();
		orgList = DAOFactory.getInstance().createOrgDAO().getOrgListByQuyuCodeWithoutC(code);
		return orgList;
	}

	public Org getOrgByCode(String code) {
		return DAOFactory.getInstance().createOrgDAO().getOrgByCodeWithoutC(code);
	}

	public Org getCOrgByCode(String code) {
		if (code != null && (code.trim().startsWith("C") || code.trim().startsWith("c"))) {
			return DAOFactory.getInstance().createOrgDAO().getCOrgByCode(code);
		} else {
			return null;
		}
	}

	public Org getOrgByCodeStaffCode(String code, String staffCode) {
		List<Org> orgList = null;
		if (staffCode.equals(Constants.ADMINISTRATOR_CODE)) {
			Org org = DAOFactory.getInstance().createOrgDAO().getOrgByCodeWithoutC(code);
			if (org != null) {
				return org;
			}
		} else {
			orgList = DAOFactory.getInstance().createOrgDAO().getOrgByCodeStaffCodeWithoutC(code, staffCode);
		}
		if (orgList != null && orgList.size() > 0) {
			return orgList.get(0);
		}
		return null;
	}
}
